#include <stdio.h>
#include <stdlib.h>                                                         /*用来调用system("cls")来清屏*/       
#include <unistd.h>                                                         /*用来调用 usleep()来延迟输出*/
#define N 30
#define HOUSE_NUM 64                                                         /*结构体数组中房屋个数*/                  

struct house{                                                                /*房屋信息的结构体类型*/
    int Building_number;                                                     /*楼编号*/
    int Unit_number;                                                         /*单元号*/
    int Room_number;                                                         /*房间号*/
    char Room_face[N];                                                       /*朝向（如：南北、东西、东南等）*/
    char House_type[N];                                                      /*户型*/
    int Bilting_area;                                                        /*建筑面积*/
}buildings[HOUSE_NUM]=                                                       /*定义一个结构体数组，用来储存数据*/
    {   {1,1,101,"西南","两室一厅",125},{1,1,102,"西南","三室两厅",150},
        {1,1,201,"西南","两室一厅",125},{1,1,202,"西南","三室两厅",150},
        {1,1,301,"西南","两室一厅",125},{1,1,302,"西南","三室两厅",150},
        {1,1,401,"西南","两室一厅",125},{1,1,402,"西南","三室两厅",150},
        {1,2,101,"西南","两室一厅",125},{1,2,102,"西南","三室两厅",150},
        {1,2,201,"西南","两室一厅",125},{1,2,202,"西南","三室两厅",150},
        {1,2,301,"西南","两室一厅",125},{1,2,302,"西南","三室两厅",150},
        {1,2,401,"西南","两室一厅",125},{1,2,402,"西南","三室两厅",150},

        {2,1,101,"东南","两室一厅",125},{2,1,102,"东南","三室两厅",150},
        {2,1,201,"东南","两室一厅",125},{2,1,202,"东南","三室两厅",150},
        {2,1,301,"东南","两室一厅",125},{2,1,302,"东南","三室两厅",150},
        {2,1,401,"东南","两室一厅",125},{2,1,402,"东南","三室两厅",150},
        {2,2,101,"东南","两室一厅",125},{2,2,102,"东南","三室两厅",150},
        {2,2,201,"东南","两室一厅",125},{2,2,202,"东南","三室两厅",150},
        {2,2,301,"东南","两室一厅",125},{2,2,302,"东南","三室两厅",150},
        {2,2,401,"东南","两室一厅",125},{2,2,402,"东南","三室两厅",150},

        {3,1,101,"东北","两室一厅",125},{3,1,102,"东北","三室两厅",150},
        {3,1,201,"东北","两室一厅",125},{3,1,202,"东北","三室两厅",150},
        {3,1,301,"东北","两室一厅",125},{3,1,302,"东北","三室两厅",150},
        {3,1,401,"东北","两室一厅",125},{3,1,402,"东北","三室两厅",150},
        {3,2,101,"东北","两室一厅",125},{3,2,102,"东北","三室两厅",150},
        {3,2,201,"东北","两室一厅",125},{3,2,202,"东北","三室两厅",150},
        {3,2,301,"东北","两室一厅",125},{3,2,302,"东北","三室两厅",150},
        {3,2,401,"东北","两室一厅",125},{3,2,402,"东北","三室两厅",150},

        {4,1,101,"西北","两室一厅",125},{4,1,102,"西北","三室两厅",150},
        {4,1,201,"西北","两室一厅",125},{4,1,202,"西北","三室两厅",150},
        {4,1,301,"西北","两室一厅",125},{4,1,302,"西北","三室两厅",150},
        {4,1,401,"西北","两室一厅",125},{4,1,402,"西北","三室两厅",150},
        {4,2,101,"西北","两室一厅",125},{4,2,102,"西北","三室两厅",150},
        {4,2,201,"西北","两室一厅",125},{4,2,202,"西北","三室两厅",150},
        {4,2,301,"西北","两室一厅",125},{4,2,302,"西北","三室两厅",150},
        {4,2,401,"西北","两室一厅",125},{4,2,402,"西北","三室两厅",150},
    };

void Input_data(struct house buildings[HOUSE_NUM]);                         /*录入数据功能函数声明*/
void Print_All_information(struct house buildings[HOUSE_NUM]);              /*输出所有房屋信息函数声明*/
void Looking_for(struct house buildings[HOUSE_NUM]);                        /*查询功能函数声明*/
void welcome();                                                             /*欢迎界面函数声明*/
void Move_mouse(int r,int l);                                               /*移动光标*/

int main()
{
    int n;
    do{
        welcome();                                                           /*欢迎界面函数*/
        Move_mouse(5,4);                                                     /*将光标向右移动5格，向下移动4格*/

        printf("请输入1~4选择您需要的功能\n     :");
        scanf("%d",&n);
        fflush(stdin);                                                       /*清空输入缓冲区，确保不影响后面的数据读取*/

        if(n==1){
            system("cls");                                                   /*清屏，让界面更美观*/
            printf("开启录入数据功能!!!!!!\n\n");
        }else if(n==2){
            system("cls");;                                                  /*清屏*/
            printf("                                                        开启查看所有信息功能!!!!!!\n\n");
        }else if(n==3){
            system("cls");;                                                  /*清屏*/
            printf("开启查询功能!!!!!!\n\n");
        }else if(n==4){
            system("cls");;                                                  /*清屏*/
            Move_mouse(30,6);
            printf("&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&\n\n\n");Move_mouse(30,0);
            printf("&----------------------------感谢您的使用------------------------------&\n\n\n");Move_mouse(30,0);
            printf("&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&\n\n\n");Move_mouse(30,0);
            printf("                              BYE BYE!     /(ㄒoㄒ)/~~     \n\n\n\n");Move_mouse(30,0);
            usleep(1000000);                                                 /*等待秒*/
            printf("-----------五秒后自动退出");
            usleep(5000000);                                                 /*等待5秒*/
            system("cls");
            return 0;
        }else{
            system("cls");
            printf(" \n!!!服务未开启!!!\n\n");
        }

        switch(n){
        case 1: Input_data(buildings);break;                                 /*调用录入数据功能函数*/
        case 2: Print_All_information(buildings);break;                      /*调用输出所有房屋信息函数*/
        case 3: Looking_for(buildings);break;                                /*调用查询功能函数*/
        }

        if(n!=4) printf("\n\n\n\n按任意键返回主菜单\n:");
        getchar();                                                           /*等待用户输入一个字符*/
        fflush(stdin);                                                       /*清空输入缓冲区*/
    }while(n!=4);

    return 0;
}


void Input_data(struct house buildings[HOUSE_NUM]){
    int building_number;                                                     /*用户输入的楼盘号*/
    int unit_number;                                                         /*用户输入的单元号*/
    int room_number;                                                         /*用户输入的房屋号*/
    char p='n';                                                              /*作为while循环是否结束的标志，将p赋值为'y'or'Y'循环结束，否则重新输入*/

    while(p!=89&&p!=121){
        
        p='n';
        Move_mouse(0,10);                                                    /*光标向右移动0个单位，向下移动10行*/
        printf("来输入楼号吧\\(￣︶￣*\\)),\n现有的楼编号有 '1' '2' '3' '4' \n\n\n");
        printf("请输入您想录入房屋的 \"楼编号\" \n:");
        scanf("%d",&building_number);                                        /*让用户输入楼盘号*/
        fflush(stdin);                                                       /*清空输入缓冲区，确保不影响后面的数据读取*/
        system("cls");                                                       /*清屏*/

        Move_mouse(0,10);
        printf("来输入单元号吧\\(￣︶￣*\\)),\n现有的单元号有 '1' '2' \n\n\n");
        printf("请输入您想录入房屋的 \"单元号\" \n:");
        scanf("%d",&unit_number);                                            /*让用户输入单元号*/
        fflush(stdin);                                                       /*清空输入缓冲区，确保不影响后面的数据读取*/
        system("cls");                                                       /*清屏*/

        Move_mouse(0,10);
        printf("来输入房屋号吧\\(￣︶￣*\\)),\n现有的房间号 '101' '102' '201' '202' '301' '302' '401' '402'\n\n\n");
        printf("请输入您想录入房屋的 \"房间号\" \n:");
        scanf("%d",&room_number);                                            /*让用户输入房屋号*/
        fflush(stdin);                                                       /*清空输入缓冲区，确保不影响后面的数据读取*/
        system("cls");                                                       /*清屏*/

        Move_mouse(0,10);
        printf("你希望录入%d号楼%d单元%d的信息，对吗?\n\n\n",building_number,unit_number,room_number);
        printf("输入'y'或'Y'开始录入信息，(#_<-)输入 \"其他任意按键\" 将重新输入\n:");
        scanf("%c",&p);                                                      /*给p赋值，判断while是否继续（重新输入)*/
        fflush(stdin);

        if(p!=89&&p!=121){                                                   /*如果用户输入不是'y'或'Y',输出重新输入的提示*/
            system("cls");                                        
            printf("即将重新输入......");
            usleep(3000000);
        }
        system("cls");
    }
        int i,t=0;;
        for(i=0;i<HOUSE_NUM;i++){
            if(buildings[i].Building_number==building_number&&buildings[i].Unit_number==unit_number&&buildings[i].Room_number==room_number){
                                                                            /*找到结构体数组中的楼盘号，单元号，房屋号与用户输入一致某行，执行下面的程序*/
                t=1;                                                        /*找到房屋的标志*/
                Move_mouse(0,10);
                printf("现有 '西南' '东南' '西北' '东北' 四种房间朝向\n");
                printf("请为%d号楼%d单元%d输入房间朝向\n\n:",building_number,unit_number,room_number);
                scanf("%s",buildings[i].Room_face);                         /*给找到的房屋输入朝向*/
                system("cls");

                Move_mouse(0,10);
                printf("现有 '两室一厅' '三室两厅' 两种户型\n");
                printf("请为%d号楼%d单元%d输入户型\n\n:",building_number,unit_number,room_number);
                scanf("%s",buildings[i].House_type);                        /*给找到的房屋输入户型*/
                system("cls");

                Move_mouse(0,10);
                printf("现有 '125' '150' 两种房屋大小\n");
                printf("请%d号楼%d单元%d输入房屋大小\n\n:",building_number,unit_number,room_number);
                scanf("%d",&buildings[i].Bilting_area);                     /*给找到的房屋输入大小*/
                system("cls");
                fflush(stdin);

                Move_mouse(0,10);
                printf("hhh 录入成功了＜（＾－＾）＞\n\n");                 /*输入成功的信息*/
                printf("您为%d号楼%d单元%d 录入的 朝向为%s, 户型为%s，大小为%d平方米。\n",building_number,unit_number,room_number,
                buildings[i].Room_face,buildings[i].House_type,buildings[i].Bilting_area);
                break;
            }
        }
        if(t==0){                                                           /*如果没有找到，输出相应信息*/
            Move_mouse(0,10);
            printf("对不起，您输入的房间不存在\n");
        }
    
}

void Print_All_information(struct house buildings[HOUSE_NUM]){
    int i,j=148,k=148;
    
    while(j--)printf("-");                                                  /*输出148个 '-' */
    printf("\n\n");
    for(i=0;i<HOUSE_NUM;i++){                                               /*遍历输出结构体数组*/
        printf(" %d号楼 %d单元 %d号房间 房间朝向是%s 户型是%s 房间大小是%d平方米 |",
        buildings[i].Building_number,buildings[i].Unit_number,buildings[i].Room_number,
        buildings[i].Room_face,buildings[i].House_type,buildings[i].Bilting_area);
        usleep(15000);
        if((i+1)%2==0)printf("\n");                                         /*没输出两次，换行*/
    }
    printf("\n");

    while(k--)printf("-");                                                  /*输出148个 '-' */
}


void Looking_for(struct house buildings[HOUSE_NUM]){
    char room_face[N];                                                      /*用户输入的房屋朝向*/
    char house_type[N];                                                     /*用户输入的房屋类型*/
    char p='n';                                                             /*作为while循环是否结束的标志，将p赋值为'y'or'Y'循环结束，否则重新输入*/

    while(p!=89&&p!=121){
        Move_mouse(0,5);
        printf("现有  4种房屋朝向  |  2种户型\n\n");                        /*输出校园的房屋朝向，和户型。供用户参考*/
        printf(" *      西南       |*  两室一厅\n");
        printf(" *      东南       |*  三室两厅\n");
        printf(" *      西北       |  \n");
        printf(" *      东北       |  \n\n");
        printf("*请输入您所需的房屋朝向,我们将为您查寻满足要求的房屋\n\n");

        printf("请输入您所需的 \"房屋朝向\"\n\n:");
        scanf("%s",room_face);                                              /*输入朝向*/


        printf("请输入您所需的 \"户型\"\n\n:");
        scanf("%s",house_type);                                             /*输入户型*/
        fflush(stdin);

        printf("\n您希望找到房屋朝向是%s户型是%s,对吗？\n",room_face,house_type);
        printf("输入 y 或者 Y 将为您搜索满足要求的房屋,输入其他任意按键将重新输入\n:");
        scanf("%c",&p);
        fflush(stdin);

        if(p!=89&&p!=121){
            system("cls");
            printf("即将重新输入......");
            usleep(3000000);
        }
        system("cls");
    }

    int i=0,t=0;                                                            /* t是找到房屋的标志*/
    for(i=0;i<HOUSE_NUM;i++){                                               /*在结构体数组中找到与用户输入朝向和户型相等的房屋*/
        if(!strcmp(buildings[i].Room_face,room_face)&&!strcmp(buildings[i].House_type,house_type)){
        if(t==0) printf("为您搜索到以下房间\n");
        usleep(50000);
        
        printf("%d号楼 %d单元 %d号房间 房间朝向是%s 户型是%s 房间大小是%d平方米\n\n",     /*输出找到的房屋*/
        buildings[i].Building_number,buildings[i].Unit_number,buildings[i].Room_number,
        buildings[i].Room_face,buildings[i].House_type,buildings[i].Bilting_area);

        t=1;                                                                /*找到房屋令t=1*/
        }
    }

    if(t==0){                                                               /*如果未找到相应房屋，输出相应信息*/
        Move_mouse(0,5);
        printf("对不起，未找到满足您要求的房屋 <(＿ ＿)>\n");
    }
}


void welcome(){
    system("cls");                                                          /*清屏，让界面更美观*/
    Move_mouse(10,10);
    int i=100,j=100;

    while(i--) printf("*");                                                 /*输出100个 '*' */

    printf("\n\n                                               尊敬的管理者，(＾Ｕ＾)ノ~嗨\n");
    printf("\n                                               欢迎进入楼盘信息管理系统\n\n");

    Move_mouse(10,0);   

    while(j--)printf("*");                                                  /*输出100个 '*' */
   
    printf("\n\n          --------------------------------------我们将为您提供以下功能---------------------------------------\n\n\n");
    printf("                 [1]:录入房屋信息           [2]:显示所有房屋信息        [3]: 输入朝向、户型，显示符合的所有房屋\n\n\n");
    printf("                                                                                              ＞﹏＜  [4]: 退出\n\n");
}


void Move_mouse(int r,int l){
    while(l--){
        printf("\n");                                                        /* 'l'的值为下移多少行*/
    }
        while(r--){
        printf(" ");                                                         /* 'r'的值为右移多少格*/
    }
}


